/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.afollestad.materialdialogssample;

import com.afollestad.materialdialogs.utils.UiUtil;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

public class ButtonItemAdapter extends BaseItemProvider {
    private final CharSequence[] mItems;
    private ItemCallback mItemCallback;
    private ButtonCallback mButtonCallback;
    private Context mContext;

    public ButtonItemAdapter(Context context, CharSequence[] items) {
        mContext = context;
        mItems = items;
    }

    public void setCallbacks(ItemCallback itemCallback, ButtonCallback buttonCallback) {
        mItemCallback = itemCallback;
        mButtonCallback = buttonCallback;
    }

    public interface ItemCallback {
        void onItemClicked(int itemIndex);
    }

    public interface ButtonCallback {
        void onButtonClicked(int buttonIndex);
    }

    @Override
    public int getCount() {
        return mItems.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dialog_customlistitem, componentContainer, false);
        }
        onPrepareView(index, component);
        return component;
    }

    private void onPrepareView(int index, Component component) {
        Text title = UiUtil.getComponent(component, ResourceTable.Id_md_title);
        if (title != null) {
            title.setText(mItems[index] + " (" + index + ")");
        }
        Button button = UiUtil.getComponent(component, ResourceTable.Id_md_button);
        if (button != null) {
            button.setBackground((new UiUtil(mContext)).getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
            button.setTag(index);
            button.setClickedListener(view -> onItemClicked(view, index));
        }
        component.setClickedListener(view -> onItemClicked(view, index));
    }

    private void onItemClicked(Component component, int index) {
        if (mItemCallback == null) {
            return;
        }
        if (component instanceof Button) {
            mButtonCallback.onButtonClicked(index);
        } else {
            mItemCallback.onItemClicked(index);
        }
    }
}
